import type { RouteRecordRaw } from "vue-router";
// 引入路由对象
import router from "../router";
type IRoute = RouteRecordRaw & { id?: string }
// 定义全部路由：Admin 下的子一级

const allRoutes: IRoute[] = [
    {
        id: "2-1",
        name: "BannerList",
        path: "BannerList",
        component: () => import("../views/Admin/Banner/List.vue")
    },
    {
        id: "2-2",
        name: "BannerAdd",
        path: "BannerAdd",
        component: () => import("../views/Admin/Banner/Add.vue")
    },
    {
        id: "2-3",
        name: "BannerDel",
        path: "BannerDel",
        component: () => import("../views/Admin/Banner/Del.vue")
    },
    {
        id: "3-1",
        name: "AdminList",
        path: "AdminList",
        component: () => import("../views/Admin/Admin/List.vue")
    },
    {
        id: "3-2",
        name: "AdminAdd",
        path: "AdminAdd",
        component: () => import("../views/Admin/Admin/Add.vue")
    },
    {
        id: "4-1",
        name: "GoodCategory",
        path: "GoodCategory",
        component: () => import("../views/Admin/Goods/Category.vue")
    },
    {
        id: "4-2",
        name: "GoodPage",
        path: "GoodPage",
        component: () => import("../views/Admin/Goods/GoodsWithPage.vue")
    },
    {
        id: "4-3",
        name: "GoodSeckillOrRecommend",
        path: "GoodSeckillOrRecommend",
        component: () => import("../views/Admin/Goods/LookSeckillOrRecommend.vue")
    },
    {
        id: "5-1",
        name: "SearchKeywords",
        path: "SearchKeywords",
        component: () => import("../views/Admin/Search/SearchKeywords.vue")
    },
    {
        id: "6-1",
        name: "User",
        path: "User",
        component: () => import("../views/Admin/User/User.vue")
    },
    {
        id: "6-2",
        name: "AddressList",
        path: "AddressList",
        component: () => import("../views/Admin/User/AddressList.vue")
    },
    {
        id: "7-1",
        name: "OrderList",
        path: "OrderList",
        component: () => import("../views/Admin/Order/OrderList.vue")
    },
    {
        id: "7-2",
        name: "LookStatusOrder",
        path: "LookStatusOrder",
        component: () => import("../views/Admin/Order/LookStatusOrder.vue")
    },
    {
        id: "8-1",
        name: "CartList",
        path: "CartList",
        component: () => import("../views/Admin/Cart/CartList.vue")
    },
    {
        id: "9-1",
        name: "LinearKChart",
        path: "LinearKChart",
        component: () => import("../views/Admin/Chart/LinearKChart.vue")
    },
    {
        id: "9-2",
        name: "SimpleChart",
        path: "SimpleChart",
        component: () => import("../views/Admin/Chart/SimpleChart.vue")
    }
]

export default function setRoute() {
    let checkedkeys: string[] = []
    let currentkeys = sessionStorage.getItem("grants")
    if (currentkeys) {
        checkedkeys = JSON.parse(currentkeys)
    }
    if (checkedkeys.length === 0) {
        checkedkeys = ["1", "2", "2-1", "2-1-R", "2-1-U", "2-2", "2-3", "2-3-R", "2-3-D", "3", "3-1", "3-1-R", "3-1-D", "3-1-U", "3-2", "4", "4-1", "4-2", "4-3", "5", "5-1", "6", "6-1", "6-2", "7", "7-1", "7-1-R", "7-1-U", "7-2", "8", "8-1", "9", "9-1", "9-2"]
    }
    // 获取当前用户路由配置
    allRoutes.forEach(item => {
        if (checkedkeys.includes(item.id as string)) {
            // 动态添加路由配置 vueRouter 对象.addRoute(父级路由的name,路由配置)
            let currRoute = { ...item }
            let arr = ["0", "0", "0", "0"]
            if (checkedkeys.includes(item.id + "-C")) {
                arr[0] = "1"
            }
            if (checkedkeys.includes(item.id + "-R")) {
                arr[1] = "1"
            }
            if (checkedkeys.includes(item.id + "-U")) {
                arr[2] = "1"
            }
            if (checkedkeys.includes(item.id + "-D")) {
                arr[3] = "1"
            }
            currRoute.meta = {
                crud: arr.join("")
            }
            router.addRoute("Home", currRoute)
        }
    })
    console.log(router.getRoutes());
}
// 清除路由 参数 路由的name
// router.removeRoute(item.name as string);
